<?php

include_once './app/view/webView.php' ;
include_once './app/model/userModel.php' ;

class accountController extends Controller
{
	
	public function __construct ( )
	{
		
		parent::__construct ( ) ;
		
		$this->filter['before'] = array ( 'connection_verif' => 'ALL') ;
		
	}
	
	public function connection_verif ( )
	{
		
		if ( isset ( $this->session['user'] ) )
		{
				
			$this->data['logged'] = true ;
			
			$user = userModel::getById ( $this->session['user'] ) ;
			
			$this->data['first_name'] = $user->getFirstName ( ) ;
			$this->data['last_name'] = $user->getLastName ( ) ;
			
			unset ( $user ) ;
				
		}
		else
		{
				
			System::redirectTo ( ) ;
				
		}
		
	}
	
	public function home ( )
	{
		
		// Afficher la liste des comptes de l'utilisateur
		$user = userModel::getById ( $this->session['user'] ) ;
		$accounts_src = $user->getAccounts ( ) ;
		
		$accounts_res = array ( 'total' => 1, 'page' => 1, 'records' => count ( $accounts_src ), 'rows' => array ( ) ) ;
		
		if ( $accounts_res['records'] != 0 )
			foreach ( $accounts_src as $acc )
			{
				
				$type = typeModel::getById ( $acc->getType ( ) ) ;
				
				$row = array ( 'id' => $acc->getId ( ), 'cell' => array ( $acc->getAccountNumber ( ), $type->getEntitled( ), number_format ( $acc->getAmount ( ), 2, ',', ' ' ) . ' €' ) ) ;
				
				$accounts_res['rows'][] = $row ;
				
			}
			
		$this->data['title'] = 'Liste de vos comptes' ;
		$this->data['accounts'] = str_replace( '"', '\"', json_encode($accounts_res) ) ;
		$this->render ( 'webView', 'account_list', 'layout' ) ;
		
	}
	
	public function view ( )
	{
		
		// Afficher toutes les infos d'un comptes en particulier avec l'historique des transactions !
		if ( ! isset ( $this->params['id'] ) )
			System::redirectTo ( 'account' ) ;
		
		$account = accountModel::getById( intval ( $this->params['id'] ) ) ;
		
		if ( $account == null )
			System::redirectTo ( 'account' ) ;
		
		if ( $account->getOwner ( ) != $this->session['user'] )
			System::redirectTo ( 'account' ) ;
		
		$type = typeModel::getById ( $account->getType ( ) ) ;
		$transactions = $account->getTransactions ( ) ;
		
		// Liste des autres comptes pour faire un virement
		$his_accounts = accountModel::getByOwner( $this->session['user'] ) ;
		$this->data['otheraccounts'] = array ( ) ;
		
		foreach ( $his_accounts as $acc )
		{
			
			if ( $acc->getId ( ) != $account->getId ( ) )
			{
				
				$type2 = typeModel::getById( $acc->getType ( ) ) ;
				
				$this->data['otheraccounts'][$acc->getId ( )] = array (
																'type' => $type2->getEntitled ( ),
																'bankcode' => BANK_CODE,
																'cashdeskcode' => $acc->getCashDeskCode ( ),
																'accountnumber' => $acc->getAccountNumber ( ),
																'ribkey' => $acc->getRIBKey ( )
															) ;
				
			}
			
		}
		
		$this->data['title'] = 'Détails de compte' ;
		$this->data['id'] = $account->getId ( ) ;
		$this->data['creation_date'] = $account->getCreationDate ( ) ;
		$this->data['amount'] = number_format ( $account->getAmount ( ), 2, ',', ' ' ) . ' €' ;
		$this->data['entitled'] = $type->getEntitled ( ) ;
		$this->data['ceiling'] = ( $type->getCeiling ( ) ) ? number_format ( $type->getCeiling ( ), 2, ',', ' ' ) . ' €' : 'Aucun' ;
		$this->data['withdraw'] = $type->getWithdraw ( ) ;
		$this->data['overdraft'] = ( $type->getOverdraft ( ) != null ) ? number_format ( $type->getOverdraft ( ), 2, ',', ' ' ) . ' €' : 'Aucune limite' ;
		
		$this->render ( 'webView', 'account_view', 'layout' ) ;
		
	}
	
	public function trs ( )
	{
		
		if ( ! isset ( $this->params['id'] ) )
			System::redirectTo ( 'account' ) ;
		
		$account = accountModel::getById( intval ( $this->params['id'] ) ) ;
		
		if ( $account == null )
			exit ( ) ;
		
		if ( $account->getOwner ( ) != $this->session['user'] )
			exit ( ) ;
		
		if ( ! isset ( $this->params['page'] ) || ! isset ( $this->params['rows'] ) )
			exit ( ) ;
		
		$trs = $account->getTransactions ( ) ;
		
		$res_trs = array ( ) ;
		
		$start = count ( $trs ) - ( $this->params['page'] - 1 ) * $this->params['rows'] - 1 ;
		$end = $start - $this->params['rows'] + 1 ;
		if ( $end < 0 ) $end = 0 ;
		
		for ( $i = $start ; $i >= $end ; $i -- )
		{
			
			$res_trs[] = array (
				'id' => $trs[$i]->getId ( ),
				'cell' => array (
					$trs[$i]->getDate ( ),
					$trs[$i]->getEntitled ( ),
					number_format ( $trs[$i]->getAmount ( ), 2, ',', ' ' ) . ' €'
				)
			) ;
			
		}
		
		$res = array (
			'total' => ceil ( count ( $trs ) / intval ( $this->params['rows'] ) ),
			'page' => intval ( $this->params['page'] ),
			'records' => count ( $trs ),
			'rows' => $res_trs
		) ;
		
		echo json_encode ( $res ) ; exit ;
		
	}
	
	public function rib ( )
	{
		
		if ( ! isset ( $this->params['id'] ) )
			System::redirectTo ( 'account' ) ;
		
		$account = accountModel::getById( intval ( $this->params['id'] ) ) ;
		
		if ( $account == null )
			exit ( ) ;
		
		if ( $account->getOwner ( ) != $this->session['user'] )
			exit ( ) ;
		
		$client = userModel::getById( $this->session['user'] ) ;
		
		$this->data['nom'] = $client->getLastName ( ) ;
		$this->data['prenom'] = $client->getFirstName( ) ;
		$this->data['code_guichet'] = $account->getCashDeskCode( ) ;
		$this->data['numero_compte'] = $account->getAccountNumber( ) ;
		$this->data['cle_rib'] = $account->getRIBKey( ) ;
		
		$this->render ( 'webView', 'rib_view' ) ;
		
	}
	
	public function transfer ( )
	{
		
		$validator = new KValidator ( 'transfer_form', KValidator::POST ) ;
		
		$sent = $validator->sent ( ) ;
		$valid = $validator->valid ( ) ;
		
		if ( ! $sent || ! $valid )
			$this->data['text'] = 0 ;
		else
		{
			
			$account_debit = accountModel::getById( $validator->getValue( 'account' ) ) ;
			$type_debit = typeModel::getById( $account_debit->getType( ) ) ;
			
			if ( $account_debit == null )
				exit ( ) ;
			
			if ( $account_debit->getOwner ( ) != $this->session['user'] )
				exit ( ) ;
			
			$amount = floatval ( $validator->getValue( 'amount' ) ) ;
			
			if ( $amount <= 0 )
				$this->data['text'] = 0 ;
			else
			{
				
				if ( $type_debit->getWithdraw( ) )
				{
					
					if ( ( -1 * $type_debit->getOverdraft ( ) ) <= ( $account_debit->getAmount() - $amount ) )
					{
						
						$trs_debit = new transactionModel() ;
						$trs_debit->setAmount( -1 * $amount ) ;
						$trs_debit->setEntitled( 'Débit: ' . $validator->getValue( 'comment' ) ) ;
						$trs_debit->setDate( date ( 'Y-m-d' ) ) ;
						
						if ( $validator->getValue( 'bankcode' ) == BANK_CODE )
						{
							
							$account_credit = accountModel::getByRIB( $validator->getValue( 'cashdeskcode' ), $validator->getValue( 'acountnumber' ), $validator->getValue( 'ribkey' ) ) ;
							
							if ( $account_credit != null )
							{
								
								$type_credit = typeModel::getById( $account_credit->getType( ) ) ;
								
								if ( $type_credit->getCeiling() == null || ( $account_credit->getAmount() + $amount ) < $type_credit->getCeiling( ) )
								{
									
									$trs_credit = new transactionModel() ;
									$trs_credit->setAmount( $amount ) ;
									$trs_credit->setEntitled( 'Crédit: ' . $validator->getValue( 'comment' ) ) ;
									$trs_credit->setDate( date ( 'Y-m-d' ) ) ;
									
									$trs_debit->setAccount( $account_debit->getId() ) ;
									$trs_debit->save( ) ;
									
									$trs_credit->setAccount( $account_credit->getId() ) ;
									$trs_credit->save( ) ;
									
									$this->data['text'] = 5 ;
									
								}
								else
								{
									
									$this->data['text'] = 4 ;
									
								}
								
							}
							else
							{
								
								$this->data['text'] = 3 ;
								
							}
							
						}
						else
						{
							
							$trs_debit->setAccount( $account_debit->getId() ) ;
							$trs_debit->save( ) ;
							
							$this->data['text'] = 10 ;
							
						}
						
					}
					else
					{
						
						$this->data['text'] = 2 ;
						
					}
					
				}
				else
				{
					
					$this->data['text'] = 1 ;
					
				}
				
			}
			
		}
		
		unset ( $validator, $sent, $valid ) ;
		
		$this->render ( 'webView', 'transfer_return' ) ;
		
	}
	
}

?>